import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig(({mode}) => {
    // 加载当前模式下的环境变量（自动读取 .env.[mode]）
    const env = loadEnv(mode, process.cwd(), '');
    console.log(env.BACKEND_API_BASE_URL)
    return {
        plugins: [vue()],
        server: {
            port: 9090,
            proxy: {
                [env.VITE_PROXY_API_PREFIX]: {
                    target: env.VITE_BACKEND_API_BASE_URL,
                    changeOrigin: true,
                    rewrite: (path) => {
                        const newPath = path.replace(new RegExp("^\\" + env.VITE_PROXY_API_PREFIX), '')
                        console.log(`路径替换: ${path} → ${newPath}`);
                        return newPath;
                    },
                },
            },
        }
    };
});